package domain;

/**
 * 域对象的定义采用符合EJB3标准的注解方式定义 建议数据库字段的命名符合数据库的习惯,即用"_"的方式分词,并且Table以T_开始,所有字母大写
 * 字段命名采用类似USER_NAME的大写分词方式 对于字符型的字段必须定义其长度，如无特殊要求，字符型定义为VARCHAR 建议定义字段是否允许为空
 * 常用的查询语句建议采用NamedQuery方式定义 建议属性的getXX()方法统一放置在前面,setXX()方法放置在末尾
 * 一般来说，域对象的主键（Identity）采用自动增长的Integer类型 并定义为id
 * (column="ID")，如果存在业务主键，则业务主键作为普通的字段定义并添加unique约束
 */
import java.io.Serializable;
import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;

import beans.Role;

/**
 * 域对象:用户帐号,代表使用该系统的用户登陆账号.
 * @author spring
 */

@Entity
@Table(name = "T_USER_ACCOUNT")
@NamedQuery(name = "UserAccount.getAllUsers", query = "select u from UserAccount u")
public class UserAccount implements Serializable {

    /**
     * 用户角色表.
     */
    public static final Map<String, Role> ROLE_MAP = new HashMap<String, Role>();
    static {
        ROLE_MAP.put("user", new Role("user", "普通用户"));
        ROLE_MAP.put("admin", new Role("admin", "高级用户"));
    }

    /**
     * serialVersionUID.
     */
    private static final long serialVersionUID = -7764820051633492442L;

    /**
     * 登陆账号.
     */
    private String userID;

    /**
     * 用户昵称.
     */
    private String userName;

    /**
     * 用户密码.
     */
    private String password;

    /**
     * 账号创建日期�û�����ʱ��.
     */
    private Date createDate;

    /**
     * 最后一次修改密码时间.
     */
    private Date passwordLastChangeDate;

    /**
     * 账号角色.
     */
    private String roleID;

    /**
     * @return userID.
     */
    @Id
    @Column(name = "USER_ID")
    public String getUserID() {
        return userID;
    }

    /**
     * @return Returns the userName.
     */
    @Column(name = "USER_NAME", length = 16)
    public String getUserName() {
        return userName;
    }

    /**
     * @return password.
     */
    @Column(name = "PASSWORD", length = 16)
    public String getPassword() {
        return password;
    }

    /**
     * @return roleID.
     */
    @Column(name = "ROLE_ID", length = 32)
    public String getRoleID() {
        return roleID;
    }

    /**
     * @return createDate.
     */
    @Column(name = "CREATE_DATE")
    public Date getCreateDate() {
        return createDate;
    }

    /**
     * @return createDate.
     */
    @Column(name = "PASSWORD_LASTCHANGE_DATE")
    public Date getPasswordLastChangeDate() {
        return passwordLastChangeDate;
    }

    /**
     * @param userid the user id to set.
     */
    public void setUserID(String userid) {
        this.userID = userid;
    }

    /**
     * @param pCreateDate create date.
     */
    public void setCreateDate(Date pCreateDate) {
        this.createDate = pCreateDate;
    }

    /**
     * @param pPasswordLastChangeDate password last change date.
     */
    public void setPasswordLastChangeDate(Date pPasswordLastChangeDate) {
        this.passwordLastChangeDate = pPasswordLastChangeDate;
    }

    /**
     * @param role the role to set.
     */
    public void setRoleID(String role) {
        this.roleID = role;
    }

    /**
     * @param pPassword password to set.
     */
    public void setPassword(String pPassword) {
        this.password = pPassword;
    }

    /**
     * @param pUserName user name to set.
     */

    public void setUserName(String pUserName) {
        this.userName = pUserName;
    }

    /**
     * 取得角色显示名 .
     * @return 角色显示名.
     */
    @Transient
    public String getRoleDisplayName() {
        Role role = ROLE_MAP.get(this.roleID);
        return (role != null) ? role.getDisplayName() : null;
    }

}
